{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np  # 矩阵操作\n",
    "import pandas as pd # SQL数据处理\n",
    "\n",
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "\n",
    "import matplotlib.pyplot as plt   #画图\n",
    "import seaborn as sns\n",
    "\n",
    "# 图形出现在Notebook里而不是新窗口\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('/home/rockdong/homework1_FE_AmesHousePrice/homework1_AmesHousePrice的副本/AmesHouse_FE_train.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查看数据信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>334</th>\n",
       "      <th>335</th>\n",
       "      <th>336</th>\n",
       "      <th>337</th>\n",
       "      <th>338</th>\n",
       "      <th>339</th>\n",
       "      <th>340</th>\n",
       "      <th>341</th>\n",
       "      <th>342</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>1.456000e+03</td>\n",
       "      <td>...</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.0</td>\n",
       "      <td>1456.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>4.614166e-14</td>\n",
       "      <td>-3.271926e-14</td>\n",
       "      <td>2.301055e-13</td>\n",
       "      <td>9.260926e-14</td>\n",
       "      <td>-9.269382e-13</td>\n",
       "      <td>6.070262e-14</td>\n",
       "      <td>-7.155734e-14</td>\n",
       "      <td>1.076807e-12</td>\n",
       "      <td>-2.059672e-13</td>\n",
       "      <td>2.256631e-13</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>180151.233516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>1.000344e+00</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>76696.592530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-1.691837e+00</td>\n",
       "      <td>-9.281155e-01</td>\n",
       "      <td>-1.554563e+01</td>\n",
       "      <td>-2.433785e-01</td>\n",
       "      <td>-4.486961e+00</td>\n",
       "      <td>-3.814446e+01</td>\n",
       "      <td>-7.007295e+00</td>\n",
       "      <td>-2.641321e+00</td>\n",
       "      <td>-2.696860e+00</td>\n",
       "      <td>-3.285242e+00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>34900.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-4.516607e-01</td>\n",
       "      <td>-2.952139e-01</td>\n",
       "      <td>6.432675e-02</td>\n",
       "      <td>-2.433785e-01</td>\n",
       "      <td>-1.028509e+00</td>\n",
       "      <td>2.621613e-02</td>\n",
       "      <td>2.260418e-01</td>\n",
       "      <td>-6.940990e-01</td>\n",
       "      <td>-4.192334e-01</td>\n",
       "      <td>-5.692198e-01</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>129900.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.684272e-01</td>\n",
       "      <td>-9.944678e-02</td>\n",
       "      <td>6.432675e-02</td>\n",
       "      <td>-2.433785e-01</td>\n",
       "      <td>7.007165e-01</td>\n",
       "      <td>2.621613e-02</td>\n",
       "      <td>2.260418e-01</td>\n",
       "      <td>-6.940990e-01</td>\n",
       "      <td>-4.192334e-01</td>\n",
       "      <td>2.698005e-02</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>163000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>6.408751e-01</td>\n",
       "      <td>1.155699e-01</td>\n",
       "      <td>6.432675e-02</td>\n",
       "      <td>-2.433785e-01</td>\n",
       "      <td>7.007165e-01</td>\n",
       "      <td>2.621613e-02</td>\n",
       "      <td>2.260418e-01</td>\n",
       "      <td>1.253123e+00</td>\n",
       "      <td>-4.192334e-01</td>\n",
       "      <td>9.544021e-01</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>214000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>7.550426e+00</td>\n",
       "      <td>2.077593e+01</td>\n",
       "      <td>6.432675e-02</td>\n",
       "      <td>5.125698e+00</td>\n",
       "      <td>7.007165e-01</td>\n",
       "      <td>2.621613e-02</td>\n",
       "      <td>2.260418e-01</td>\n",
       "      <td>1.253123e+00</td>\n",
       "      <td>1.858393e+00</td>\n",
       "      <td>1.285624e+00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>625000.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 344 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  0             1             2             3             4  \\\n",
       "count  1.456000e+03  1.456000e+03  1.456000e+03  1.456000e+03  1.456000e+03   \n",
       "mean   4.614166e-14 -3.271926e-14  2.301055e-13  9.260926e-14 -9.269382e-13   \n",
       "std    1.000344e+00  1.000344e+00  1.000344e+00  1.000344e+00  1.000344e+00   \n",
       "min   -1.691837e+00 -9.281155e-01 -1.554563e+01 -2.433785e-01 -4.486961e+00   \n",
       "25%   -4.516607e-01 -2.952139e-01  6.432675e-02 -2.433785e-01 -1.028509e+00   \n",
       "50%    1.684272e-01 -9.944678e-02  6.432675e-02 -2.433785e-01  7.007165e-01   \n",
       "75%    6.408751e-01  1.155699e-01  6.432675e-02 -2.433785e-01  7.007165e-01   \n",
       "max    7.550426e+00  2.077593e+01  6.432675e-02  5.125698e+00  7.007165e-01   \n",
       "\n",
       "                  5             6             7             8             9  \\\n",
       "count  1.456000e+03  1.456000e+03  1.456000e+03  1.456000e+03  1.456000e+03   \n",
       "mean   6.070262e-14 -7.155734e-14  1.076807e-12 -2.059672e-13  2.256631e-13   \n",
       "std    1.000344e+00  1.000344e+00  1.000344e+00  1.000344e+00  1.000344e+00   \n",
       "min   -3.814446e+01 -7.007295e+00 -2.641321e+00 -2.696860e+00 -3.285242e+00   \n",
       "25%    2.621613e-02  2.260418e-01 -6.940990e-01 -4.192334e-01 -5.692198e-01   \n",
       "50%    2.621613e-02  2.260418e-01 -6.940990e-01 -4.192334e-01  2.698005e-02   \n",
       "75%    2.621613e-02  2.260418e-01  1.253123e+00 -4.192334e-01  9.544021e-01   \n",
       "max    2.621613e-02  2.260418e-01  1.253123e+00  1.858393e+00  1.285624e+00   \n",
       "\n",
       "           ...           334     335     336     337     338     339     340  \\\n",
       "count      ...        1456.0  1456.0  1456.0  1456.0  1456.0  1456.0  1456.0   \n",
       "mean       ...           1.0     1.0     1.0     1.0     1.0     1.0     1.0   \n",
       "std        ...           0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "min        ...           1.0     1.0     1.0     1.0     1.0     1.0     1.0   \n",
       "25%        ...           1.0     1.0     1.0     1.0     1.0     1.0     1.0   \n",
       "50%        ...           1.0     1.0     1.0     1.0     1.0     1.0     1.0   \n",
       "75%        ...           1.0     1.0     1.0     1.0     1.0     1.0     1.0   \n",
       "max        ...           1.0     1.0     1.0     1.0     1.0     1.0     1.0   \n",
       "\n",
       "          341     342      SalePrice  \n",
       "count  1456.0  1456.0    1456.000000  \n",
       "mean      1.0     1.0  180151.233516  \n",
       "std       0.0     0.0   76696.592530  \n",
       "min       1.0     1.0   34900.000000  \n",
       "25%       1.0     1.0  129900.000000  \n",
       "50%       1.0     1.0  163000.000000  \n",
       "75%       1.0     1.0  214000.000000  \n",
       "max       1.0     1.0  625000.000000  \n",
       "\n",
       "[8 rows x 344 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 各属性的统计信息（样本数目、均值、标准差、最小值、最大值、1/4分位数、中值（1/2分位数）、3/4分位数）\n",
    "# 只计算数值型特征的统计信息（int、float）\n",
    "\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = df['SalePrice'].values\n",
    "test = df.drop('SalePrice', axis = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 24740.56122552  25658.98582595  24227.51030455  22708.84526756\n",
      "  26900.09129371]\n",
      "[  1.28645256e+08   1.24751641e+09   3.30255970e+14   2.16394640e+10\n",
      "   4.70412926e+09]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/rockdong/anaconda2/lib/python2.7/site-packages/sklearn/linear_model/coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression, RidgeCV, LassoCV\n",
    "from pandas import DataFrame\n",
    "\n",
    "methods = {\n",
    "        'LinearRegression':LinearRegression(), \n",
    "        'RidgeCV':RidgeCV(alphas=[0.01, 0.1, 1, 10,20, 40, 80,100], store_cv_values=True),\n",
    "        'LassoCV':LassoCV(alphas=[0.01, 0.1, 1, 10,100])\n",
    "       }\n",
    "\n",
    "from sklearn.model_selection import cross_val_score\n",
    "def rmse(method, train, result):\n",
    "    rmse = np.sqrt(-cross_val_score(method, train, result, \n",
    "    scoring=\"neg_mean_squared_error\", cv = 5))\n",
    "    return rmse\n",
    "\n",
    "def fit_train(method, train, train_result, test, test_id, filename):\n",
    "    method.fit(train, train_result)\n",
    "    print rmse(method, train, train_result)\n",
    "    result_pred = method.predict(test)\n",
    "    result = DataFrame({'SalePrice': result_pred, 'id': test_id})\n",
    "    result.to_csv(filename + '.csv', index=False)\n",
    "    \n",
    "testData = pd.read_csv('/home/rockdong/homework1_FE_AmesHousePrice/homework1_AmesHousePrice的副本/AmesHouse_FE_test.csv')\n",
    "testDataID = testData['Id'].values\n",
    "testData = testData.drop('Id', axis=1)\n",
    "for method in methods:\n",
    "    fit_train(methods[method], test, result, testData, testDataID, method)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
